package cz.cvut.felk.via.sm.rest.controllers;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cv.cvut.felk.via.sm.services.FileDummyService;
import cv.cvut.felk.via.sm.services.SubjectDummyService;
import cz.cvut.felk.via.sm.model.File;


@Controller
public class FileJsonController {
FileDummyService fileDummyService;
	
	public FileDummyService getFileDummyService() {
	return fileDummyService;
}
public void setFileDummyService(FileDummyService fileDummyService) {
	this.fileDummyService = fileDummyService;
}
	@RequestMapping(value="/subjectfiles/{subject}/{week}", method=RequestMethod.GET)
	@ResponseBody
	@SuppressWarnings("unchecked")
	public List<File> getSubjectFile(@PathVariable String subject,@PathVariable String week){
		int weeki = Integer.parseInt(week);
		return fileDummyService.getSubjectFiles(subject.toUpperCase(), weeki);
	}
	@RequestMapping(value="/file/{code}",method=RequestMethod.GET)
	public ModelAndView getFiels(@PathVariable String code){
		Map<String,Object> model = new HashMap<String, Object>();
		List<File> file =fileDummyService.getSubjectFiles(code, 20);
		Collections.sort(file);
		model.put("files", file);
		model.put("code", code);
		model.put("new", new File());
		return new ModelAndView("files",model);
	}
	
	@RequestMapping(value="/file/{code}",method=RequestMethod.POST)
	public String setFiels(@PathVariable String code,File file){
//		Map<String,Object> model = new HashMap<String, Object>();
		fileDummyService.addFile(code, file);

		return "redirect:/file/"+code;
	}
}
